#!/bin/bash

# Copyright 2015 VMware, Inc.
#
# All Rights Reserved
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.


# Neutron VMware NSX plugin
# -------------------------

# Save trace setting
NSX_XTRACE=$(set +o | grep xtrace)
set +o xtrace

dir=${GITDIR['vmware-nsx']}/devstack
source $dir/lib/nsx_common
source $dir/lib/nsx_v3_p_common

function _ovsdb_connection {
    managers=(${NSX_MANAGER//,/ })
    _ovsdb_connection_common ${managers[0]}
}

function setup_integration_bridge {
    die_if_not_set $LINENO NSX_MANAGER "NSX_MANAGER has not been set!"
    setup_integration_bridge_common
}

function neutron_plugin_configure_common {
    neutron_plugin_configure_common_v3 "vmware_nsxv3"
}

function neutron_plugin_configure_service {
    nsxv3_configure_service nsx_v3
    iniset /$Q_PLUGIN_CONF_FILE DEFAULT nsx_extension_drivers vmware_nsxv3_dns
}

function init_vmware_nsx_v3 {
    if (is_service_enabled q-svc || is_service_enabled neutron-api) && [[ "$NATIVE_DHCP_METADATA" == "True" ]]; then
        if ! is_set DHCP_PROFILE_UUID; then
            die $LINENO "DHCP profile needs to be configured!"
        fi
        if ! is_set METADATA_PROXY_UUID; then
            die $LINENO "Metadata proxy needs to be configured!"
        fi
        if is_service_enabled q-dhcp q-meta; then
            die $LINENO "Native support does not require DHCP and Metadata agents!"
        fi
    fi
    # Generate client certificate
    if [[ "$NSX_USE_CLIENT_CERT_AUTH" == "True" ]]; then
        nsxadmin -o generate -r certificate
    fi
    if ! is_set NSX_GATEWAY_NETWORK_INTERFACE; then
        echo "NSX_GATEWAY_NETWORK_INTERFACE not set not configuring routes"
        return
    fi

    if ! is_set NSX_GATEWAY_NETWORK_CIDR; then
        NSX_GATEWAY_NETWORK_CIDR=$PUBLIC_NETWORK_GATEWAY/${FLOATING_RANGE#*/}
        echo "The IP address to set on $PUBLIC_BRIDGE was not specified. "
        echo "Defaulting to $NSX_GATEWAY_NETWORK_CIDR"
    fi
    # Make sure the interface is up, but not configured
    sudo ip link set $NSX_GATEWAY_NETWORK_INTERFACE up
    # Save and then flush the IP addresses on the interface
    addresses=$(ip addr show dev $NSX_GATEWAY_NETWORK_INTERFACE | grep inet | awk {'print $2'})
    sudo ip addr flush $NSX_GATEWAY_NETWORK_INTERFACE
    # Use the PUBLIC Bridge to route traffic to the NSX gateway
    get_bridge_up
}

function stop_vmware_nsx_v3 {
    # Clean client certificate if exists
    nsxadmin -o clean -r certificate

    if ! is_set NSX_GATEWAY_NETWORK_INTERFACE; then
        echo "NSX_GATEWAY_NETWORK_INTERFACE was not configured."
        return
    fi
    set_nsx_gateway_network_cidr
}

# Restore xtrace
$NSX_XTRACE
